<?php
setlocale(LC_MONETARY, 'en_US');
date_default_timezone_set('Pacific/Auckland');
$debug = 0;

// PHP Settings to display errors in the script.
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Get the ID of the proposal from the URL
$id = $_GET['id'];
$userid = $_GET['userid'];
$token = $_GET['token'];

// Username, password and schema for the database connection.
$username="aimatch";
$password="RMSAdmin";
$database="beta";

// GST (goods and services tax) and a conversion to display GST as a number without the decimal point. Also the commission percentage.
$gst=.10;
$gst_num = $gst*100;
$commission = .20;

// Connect to the database and run the query
mysql_connect('wmysql2',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

//------------------------------------------------------------
// First select the userid and token from the database
// to elimate
//------------------------------------------------------------
$security_query = "select users.id as UserID, users.persistence_token as PersistenceToken from users where users.id = ".$userid;
if($debug){ echo "Security Query: <br>".$security_query."<br>";}
// Retrieve the query results
$security_result=mysql_query($security_query);

// Determine the number of rows in the result set.
if($security_result){
        $security_num=mysql_numrows($security_result); }
else {
        $security_num = 0; }

// Retrieve the information for the header of the proposal
if($security_num){
        $i=0;
        $UserID=mysql_result($security_result,$i,"UserID");
        $PersistenceToken=mysql_result($security_result,$i,"PersistenceToken"); }
else {
        $UserID="";
        $PersistenceToken=""; }

if($debug){ echo " <b>SECURITY DETAILS </b><br>"; }
if($debug){ echo "User ID: ".$UserID."<br>"; }
if($debug){ echo "Persistence Token: ".$PersistenceToken."<br>"; }
if($debug){ echo "token: ".$token."<br>"; }
if($debug){ echo "userid: ".$userid."<br>"; }
if($debug){ echo "<hr>"; }

if ($token != $PersistenceToken) {
	die("Security Violation!!! You're USERID and Security Token do not match."); }

//------------------------------------------------------------
// Campaign Information
//------------------------------------------------------------
$campaign_query = "select proposals.id as ProposalID, proposals.name as ProposalName, proposals.start_date as ProposalStartDate, ";
$campaign_query = $campaign_query."   proposals.end_date as ProposalEndDate, proposals.advertiser_id as AdvertiserID, proposals.agency_id as AgencyID,";
$campaign_query = $campaign_query."   proposals.contact_id as ContactID, proposals.notes as ProposalNotes ";
$campaign_query = $campaign_query."from proposals ";
$campaign_query = $campaign_query."where proposals.id = ".$id;
if($debug){ echo "Campaign Query: <br>".$campaign_query."<br>";}
// Retrieve the query results
$campaign_result=mysql_query($campaign_query);

// Determine the number of rows in the result set.
if($campaign_result){
        $campaign_num=mysql_numrows($campaign_result); }
else {
        $campaign_num = 0; }

// Retrieve the information for the header of the proposal
if($campaign_num){
        $i=0;
        $ProposalID=mysql_result($campaign_result,$i,"ProposalID");
        $ProposalName=mysql_result($campaign_result,$i,"ProposalName");
        $ProposalStartDate=mysql_result($campaign_result,$i,"ProposalStartDate");
        $ProposalEndDate=mysql_result($campaign_result,$i,"ProposalEndDate");
        $AdvertiserID=mysql_result($campaign_result,$i,"AdvertiserID");
        $AgencyID=mysql_result($campaign_result,$i,"AgencyID");
        $ContactID=mysql_result($campaign_result,$i,"ContactID");
        $ProposalNotes=mysql_result($campaign_result,$i,"ProposalNotes"); }
else {
        $ProposalID="";
        $ProposalName="";
        $ProposalStartDate="";
        $ProposalEndDate="";
        $AdvertiserID="";
        $AgencyID="";
        $ContactID="";
        $ProposalNotes=""; }


if($debug){ echo " <b>CAMPAIGN DETAILS </b><br>"; }
if($debug){ echo "Proposal ID: ".$ProposalID."<br>"; }
if($debug){ echo "Proposal Name: ".$ProposalName."<br>"; }
if($debug){ echo "Proposal Start Date: ".$ProposalStartDate."<br>"; }
if($debug){ echo "Proposal End Date: ".$ProposalEndDate."<br>"; }
if($debug){ echo "Advertiser ID: ".$AdvertiserID."<br>"; }
if($debug){ echo "Agency ID: ".$AgencyID."<br>"; }
if($debug){ echo "Contact ID: ".$ContactID."<br>"; }
if($debug){ echo "Proposal Notes: ".$ProposalNotes."<br>"; }
if($debug){ echo "<hr>"; }
//------------------------------------------------------------
// Client Information
//------------------------------------------------------------
$client_query = "select contacts.full_name as ClientFullName, contacts.title as ClientTitle, contacts.email as ClientEmail, contacts.phone as ClientPhone, ";
$client_query = $client_query."   contacts.address1 as ClientAddress1, contacts.address2 as ClientAddress2, contacts.city as ClientCity, ";
$client_query = $client_query."   contacts.state as ClientState, contacts.zip as ClientZip ";
$client_query = $client_query."from contacts ";
$client_query = $client_query."where contacts.id = ".$ContactID;
if($debug){ echo "Client Query: <br>".$client_query."<br>"; }
// Retrieve the query results
$client_result=mysql_query($client_query);

// Determine the number of rows in the result set.
if($client_result){
        $client_num=mysql_numrows($client_result); }
else {
        $client_num = 0; }

if ($client_num){
        // Retrieve the information for the header of the proposal
        $i=0;
        $ClientFullName=mysql_result($client_result,$i,"ClientFullName");
        $ClientTitle=mysql_result($client_result,$i,"ClientTitle");
        $ClientEmail=mysql_result($client_result,$i,"ClientEmail");
        $ClientPhone=mysql_result($client_result,$i,"ClientPhone");
        $ClientAddress1=mysql_result($client_result,$i,"ClientAddress1");
        $ClientAddress2=mysql_result($client_result,$i,"ClientAddress2");
        $ClientCity=mysql_result($client_result,$i,"ClientCity");
        $ClientState=mysql_result($client_result,$i,"ClientState");
        $ClientZip=mysql_result($client_result,$i,"ClientZip");}
else {
        $ClientFullName="";
        $ClientTitle="";
        $ClientEmail="";
        $ClientPhone="";
        $ClientAddress1="";
        $ClientAddress2="";
        $ClientCity="";
        $ClientState="";
        $ClientZip="";}

if($debug){ echo " <b>CLIENT INFORMATION </b><br>"; }
if($debug){ echo "ContactID: ".$ContactID."<br>"; }
if($debug){ echo "Client Full Name: ".$ClientFullName."<br>"; }
if($debug){ echo "Client Title: ".$ClientTitle."<br>"; }
if($debug){ echo "Client Email: ".$ClientEmail."<br>"; }
if($debug){ echo "Client Phone: ".$ClientPhone."<br>"; }
if($debug){ echo "Client Address1 : ".$ClientAddress1."<br>"; }
if($debug){ echo "Client Address2: ".$ClientAddress2."<br>"; }
if($debug){ echo "Client City: ".$ClientCity."<br>"; }
if($debug){ echo "Client State: ".$ClientState."<br>"; }
if($debug){ echo "Client Zip: ".$ClientZip."<br>"; }
if($debug){ echo "<hr>"; }

//------------------------------------------------------------
// Agency Information
//------------------------------------------------------------
$agency_query = "select agencies.name as AgencyName, contacts.full_name as AgencyContact, contacts.title as AgencyTitle, ";
$agency_query = $agency_query."   contacts.address1 as AgencyAddress1, contacts.address2 as AgencyAddress2, contacts.city as AgencyCity, ";
$agency_query = $agency_query."   contacts.state as AgencyState, contacts.zip as AgencyZip, contacts.phone as AgencyPhone, contacts.email as AgencyEmail  ";
$agency_query = $agency_query."from agencies, contacts ";
$agency_query = $agency_query."where agencies.id = contacts.contactable_id ";
$agency_query = $agency_query."and agencies.id = ".$AgencyID;
if($debug){ echo "<b>Agency Query: </b><br>".$agency_query."<br>"; }
// Retrieve the query results
$agency_result=mysql_query($agency_query);
// Determine the number of rows in the result set.
if($agency_result){
        $agency_num=mysql_numrows($agency_result); }
else {
        $agency_num=0; }

if ($agency_num){
        // Retrieve the information for the header of the proposal
        $i=0;
        $AgencyName=mysql_result($agency_result,$i,"AgencyName");
        $AgencyContact=mysql_result($agency_result,$i,"AgencyContact");
        $AgencyTitle=mysql_result($agency_result,$i,"AgencyTitle");
        $AgencyEmail=mysql_result($agency_result,$i,"AgencyEmail");
        $AgencyPhone=mysql_result($agency_result,$i,"AgencyPhone");
        $AgencyAddress1=mysql_result($agency_result,$i,"AgencyAddress1");
        $AgencyAddress2=mysql_result($agency_result,$i,"AgencyAddress2");
        $AgencyCity=mysql_result($agency_result,$i,"AgencyCity");
        $AgencyState=mysql_result($agency_result,$i,"AgencyState");
        $AgencyZip=mysql_result($agency_result,$i,"AgencyZip");}
else {
        $AgencyName="";
        $AgencyContact="";
        $AgencyTitle="";
        $AgencyEmail="";
        $AgencyPhone="";
        $AgencyAddress1="";
        $AgencyAddress2="";
        $AgencyCity="";
        $AgencyState="";
        $AgencyZip="";}
if($debug){ echo " <b>AGENCY INFORMATION </b><br>"; }
if($debug){ echo "ContactID: ".$ContactID."<br>"; }
if($debug){ echo "Agency Name: ".$AgencyName."<br>"; }
if($debug){ echo "Acency Contact Name: ".$AgencyContact."<br>"; }
if($debug){ echo "Agency Title: ".$AgencyTitle."<br>"; }
if($debug){ echo "Agency Email: ".$AgencyEmail."<br>"; }
if($debug){ echo "Agency Phone: ".$AgencyPhone."<br>"; }
if($debug){ echo "Agency Address1 : ".$AgencyAddress1."<br>"; }
if($debug){ echo "Agency Address2: ".$AgencyAddress2."<br>"; }
if($debug){ echo "Agency City: ".$AgencyCity."<br>"; }
if($debug){ echo "Agency State: ".$AgencyState."<br>"; }
if($debug){ echo "Agency Zip: ".$AgencyZip."<br>"; }
if($debug){ echo "<hr>"; }

//------------------------------------------------------------
// Advertiser Information
//------------------------------------------------------------
$advertiser_query = "select advertisers.name as AdvertiserName ";
$advertiser_query = $advertiser_query."from advertisers ";
$advertiser_query = $advertiser_query."where advertisers.id = ".$AdvertiserID;
if($debug){ echo "Advertiser Query: <br>".$advertiser_query."<br>"; }
// Retrieve the query results
$advertiser_result=mysql_query($advertiser_query);

// Determine the number of rows in the result set.
if($advertiser_result){
        $advertiser_num=mysql_numrows($advertiser_result); }
else {
        $advertiser_num=0; }

if ($advertiser_num){
        // Retrieve the information for the header of the proposal
        $i=0;
        $AdvertiserName=mysql_result($advertiser_result,$i,"AdvertiserName");}
else {
        $AdvertiserName="";}

if($debug){ echo " <b>ADVERTISER INFORMATION </b><br>"; }
if($debug){ echo "Advertiser ID: ".$AdvertiserID."<br>"; }
if($debug){ echo "Advertiser Name: ".$AdvertiserName."<br>"; }
if($debug){ echo "<hr>"; }

//------------------------------------------------------------
// Line Item Information
//------------------------------------------------------------
$line_item_query = "select line_items.name as LineItemName, line_items.start_date as LineItemStartDate, line_items.end_date as LineItemEndDate, ";
$line_item_query = $line_item_query." line_items.impressions as LineItemImpressions, line_items.clicks as LineItemClicks, line_items.actions as LineItemActions, ";
$line_item_query = $line_item_query." line_items.bonus_impressions as LineItemBonusImpressions, line_items.bonus_clicks as LineItemBonusClicks, line_items.bonus_impressions as LineItemBonusActions, ";
$line_item_query = $line_item_query." line_items.cost_impressions as LineItemCostImpressions, line_items.cost_clicks as LineItemCostClicks, line_items.cost_actions as LineItemCostActions, ";
$line_item_query = $line_item_query." line_items.targets_group_logic as LineItemTargetsGroupLogic, line_items.daily_rev_cap as LineItemDailyRevCap, ";
$line_item_query = $line_item_query." line_items.flat_rate as LineItemFlatRate, line_items.status as LineItemStatus, products.name as LineItemProductName ";
$line_item_query = $line_item_query."from line_items, products ";
$line_item_query = $line_item_query."where line_items.product_id = products.id ";
$line_item_query = $line_item_query."and proposal_id = ".$ProposalID;

if($debug){ echo "Line Item Query: <br>".$line_item_query."<br>"; }
// Retrieve the query results
$line_item_result=mysql_query($line_item_query);

// Determine the number of rows in the result set.
if($line_item_result){
        $line_item_num=mysql_numrows($line_item_result); }
else {
        $line_item_num=0; }

if ($line_item_num){
        // Retrieve the information for the header of the proposal
        $i=0;
        while($i < $line_item_num){
                $LineItemName[$i]=mysql_result($line_item_result,$i,"LineItemName");
                $LineItemStartDate[$i]=mysql_result($line_item_result,$i,"LineItemStartDate");
                if($LineItemStartDate[$i]=="") {
                        $LineItemStartDate[$i] = "none"; }
                $LineItemEndDate[$i]=mysql_result($line_item_result,$i,"LineItemEndDate");
                if($LineItemEndDate[$i]=="") {
                        $LineItemEndDate[$i] = "none"; }
                $LineItemImpressions[$i]=mysql_result($line_item_result,$i,"LineItemImpressions");
                $LineItemClicks[$i]=mysql_result($line_item_result,$i,"LineItemClicks");
                $LineItemActions[$i]=mysql_result($line_item_result,$i,"LineItemActions");
                $LineItemBonusImpressions[$i]=mysql_result($line_item_result,$i,"LineItemBonusImpressions");
                $LineItemBonusClicks[$i]=mysql_result($line_item_result,$i,"LineItemBonusClicks");
                $LineItemBonusActions[$i]=mysql_result($line_item_result,$i,"LineItemBonusActions");
                $LineItemCostImpressions[$i]=mysql_result($line_item_result,$i,"LineItemCostImpressions");
                $LineItemCostClicks[$i]=mysql_result($line_item_result,$i,"LineItemCostClicks");
                $LineItemCostActions[$i]=mysql_result($line_item_result,$i,"LineItemCostActions");
                $LineItemTargetsGroupLogic[$i]=mysql_result($line_item_result,$i,"LineItemTargetsGroupLogic");
                $LineItemDailyRevCap[$i]=mysql_result($line_item_result,$i,"LineItemDailyRevCap");
                $LineItemFlatRate[$i]=mysql_result($line_item_result,$i,"LineItemFlatRate");
                $LineItemStatus[$i]=mysql_result($line_item_result,$i,"LineItemStatus");
                $LineItemProductName[$i]=mysql_result($line_item_result,$i,"LineItemProductName");
                $i++;
        }
}
else {
        $LineItemName[0]="";
        $LineItemStartDate[0]="";
        $LineItemEndDate[0]="";
        $LineItemImpressions[0]="";
        $LineItemClicks[0]="";
        $LineItemActions[0]="";
        $LineItemBonusImpressions[0]="";
        $LineItemBonusClicks[0]="";
        $LineItemBonusActions[0]="";
        $LineItemCostImpressions[0]="";
        $LineItemCostClicks[0]="";
        $LineItemCostActions[0]="";
        $LineItemTargetsGroupLogic[0]="";
        $LineItemDailyRevCap[0]="";
        $LineItemFlatRate[0]="";
        $LineItemStatus[0]="";
        $LineItemProductName[0]="";
        }
$i=0;
if($debug){ echo " <b>LINE ITEM INFORMATION </b><br>"; }
if($debug){ echo "Proposal ID: ".$ProposalID."<br>"; }
while ($i < $line_item_num){
        if($debug){ echo "Line Item Name: ".$LineItemName[$i]."<br>"; }
        if($debug){ echo "Line Item Start Date: ".$LineItemStartDate[$i]."<br>"; }
        if($debug){ echo "Line Item End Date: ".$LineItemEndDate[$i]."<br>"; }
        if($debug){ echo "Line Item Impressions: ".$LineItemImpressions[$i]."<br>"; }
        if($debug){ echo "Line Item Clicks: ".$LineItemClicks[$i]."<br>"; }
        if($debug){ echo "Line Item Actions: ".$LineItemActions[$i]."<br>"; }
        if($debug){ echo "Line Item Bonus Impressions: ".$LineItemBonusImpressions[$i]."<br>"; }
        if($debug){ echo "Line Item Bonus Clicks: ".$LineItemBonusClicks[$i]."<br>"; }
        if($debug){ echo "Line Item Bonus Actions: ".$LineItemBonusActions[$i]."<br>"; }
        if($debug){ echo "Line Item Cost Impresssions: ".$LineItemCostImpressions[$i]."<br>"; }
        if($debug){ echo "Line Item Cost Clicks: ".$LineItemCostClicks[$i]."<br>"; }
        if($debug){ echo "Line Item Cost Actions: ".$LineItemCostActions[$i]."<br>"; }
        if($debug){ echo "Line Item Targets Group Logic: ".$LineItemTargetsGroupLogic[$i]."<br>"; }
        if($debug){ echo "Line Item Daily Revenue Cap: ".$LineItemDailyRevCap[$i]."<br>"; }
        if($debug){ echo "Line Item Flat Rate: ".$LineItemFlatRate[$i]."<br>"; }
        if($debug){ echo "Line Item Status: ".$LineItemStatus[$i]."<br>"; }
        if($debug){ echo "Line Item Product Name: ".$LineItemProductName[$i]."<br>"; }
        if($debug){ echo "<t>----------<br>"; }
        $i++;
}
if($debug){ echo "<hr>"; }

//------------------------------------------------------------
// Billable Amount Information
//------------------------------------------------------------
$monthly_billable_query = "select billable_amount_monthly.month as BillableMonth, billable_amount_monthly.amount as BillableMonthAmount ";
$monthly_billable_query = $monthly_billable_query."from billable_amount_monthly, line_items ";
$monthly_billable_query = $monthly_billable_query."where billable_amount_monthly.line_item_id = line_items.id ";
$monthly_billable_query = $monthly_billable_query."and line_items.proposal_id = ".$ProposalID." ";
$monthly_billable_query = $monthly_billable_query."order by BillableMonth ";

if($debug){ echo "Line Item Query: <br>".$monthly_billable_query."<br>"; }
// Retrieve the query results
$monthly_billable_result=mysql_query($monthly_billable_query);

// Determine the number of rows in the result set.
if($monthly_billable_result) {
        $monthly_billable_num=mysql_numrows($monthly_billable_result); }
else {
        $monthly_billable_num=0; }

$BillableMonthAmount[0] = "";
$previousBillableMonth = "";
$y=0;	
$x=0;

if ($monthly_billable_num){
        // Retrieve the information for the header of the proposal
        $i=0;
        while($i < $monthly_billable_num){
                $tempMonth[$i]=mysql_result($monthly_billable_result,$i,"BillableMonth");
				$tempAmount[$i]=mysql_result($monthly_billable_result,$i,"BillableMonthAmount");

				if($debug){ echo "tempMonth[".$i."]: ".$tempMonth[$i]."<br>"; }
				if($debug){ echo "tempAmount[".$i."]: ".$tempAmount[$i]."<br>"; }

				if($previousBillableMonth == $tempMonth[$i]){
					$y--;
					if($debug){ echo "previousBillableMonth equals tempMonth: ".$previousBillableMonth." == ".$tempMonth[$i]."<br>"; }
					$BillableMonthAmount[$y] = $previousMonthAmount+$tempAmount[$i];
					if($debug){ echo "New BillableMonthAmount[".$y."] is: ".$BillableMonthAmount[$y]."<br>"; }
					$y++;

				}
				else {
					if($debug){ echo "New tempMonth: ".$previousBillableMonth." != ".$tempMonth[$i]."<br>"; }
					$BillableMonth[$y] = $tempMonth[$i];
					if($debug){ echo "BillableMonth[".$y."] is: ".$BillableMonth[$y]."<br>"; }
					$BillableMonthAmount[$y] = $tempAmount[$i];
					if($debug){ echo "BillableMonthAmount[".$y."] is: ".$BillableMonthAmount[$y]."<br>"; }
					$previousMonthAmount = $BillableMonthAmount[$y];
					$y++;
					$x = $y;
				}
				$previousBillableMonth = $tempMonth[$i];
                $i++;
				if($debug){ echo "===================<br>"; }
        }
}
else {
        $BillableMonth="";
        $BillableMonthAmount="";
        }
$i=0;
if($debug){ echo " <b>MONTHLY BILLABLE AMOUNT INFORMATION </b><br>"; }
if($debug){ echo "Proposal ID: ".$ProposalID."<br>"; }
while ($i < $x){
        if($debug){ echo "Billable Month: ".$BillableMonth[$i]."<br>"; }
        if($debug){ echo "Billable Month Amount: ".$BillableMonthAmount[$i]."<br>"; }
        if($debug){ echo "<t>----------<br>"; }
        $i++;
}
if($debug){ echo "<hr>"; }

// Close the mysql connection.
mysql_close();

?>